/** @format */

import { connect } from 'amqplib';

const recordTaskQueue = 'recordTask';
const recordNotiQueue = 'recordNoti';
const recordNotiExchange = 'notifacation';
const recordNotiExchangeType = 'fanout';
const recordNotiRoutingKey = '';
let index = 0;

async function bootstrap(): Promise<void> {
    const amqp = await connect('amqp://gj:123qwe@10.0.0.215:5672');
    const chTask = await amqp.createChannel();

    // autoDelete: true TCP连接断开后，队列会自动删除
    // durable: false 非持久化队列
    chTask.assertQueue(recordTaskQueue, { autoDelete: true, durable: false }); //Assert Queue 断言队列(声明队列)
    chTask.purgeQueue(recordTaskQueue); //Purge Queue 清除队列
    setInterval(() => {
        const currentMsg = `[${index++}][Task][${Date.now()}]`;
        console.log(currentMsg);
        chTask.sendToQueue(recordTaskQueue, Buffer.from(currentMsg));
    }, 1000);

    const chNoti = await amqp.createChannel();
    chNoti.assertExchange(recordNotiExchange, recordNotiExchangeType, { durable: false });
    setInterval(() => {
        const currentMsg = `[${index++}][Notifacation][${Date.now()}]`;
        console.log(currentMsg);
        chNoti.publish(recordNotiExchange, recordNotiRoutingKey, Buffer.from(currentMsg));
    }, 1000);
}
bootstrap();

/*
round robin 轮循
*/
